<template>
  <div>
    <el-row>
      <el-col :span="20">
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item>
            <el-select clearable v-model="formSearch.online_status" placeholder="请选择设备状态">
              <el-option label="在线" :value="1"></el-option>
              <el-option label="离线" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select clearable v-model="formSearch.bind_status" placeholder="请选择设备绑定状态">
              <el-option label="未绑定" :value="0"></el-option>
              <el-option label="已绑定" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input clearable v-model="formSearch.name" placeholder="请输入设备名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="toSearch">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4" style="text-align: right;">
        <el-button type="primary">录入</el-button>
      </el-col>
    </el-row>
    <div class="table">
      <el-table :data="empData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="60" align="center"></el-table-column>
        <el-table-column prop="serial_number" label="编号" width="100" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="700"></el-table-column>
        <el-table-column prop="online_status" label="在线状态" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.online_status == 1" type="success">在线</el-tag>
            <el-tag v-else type="danger">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态" align="center">
          <template v-slot="scope">
            <el-tag v-if="scope.row.bind_status == 0" type="danger">未绑定</el-tag>
            <el-tag v-else type="success">已绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-link style="font-size: 12px;" :underline="false" type="primary" @click="toEdit(scope.row)">修改</el-link>
            <el-link style="margin-left: 5px;font-size: 12px;" :underline="false" type="danger" @click="toDelete(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-drawer
      title="修改设备信息"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <div class="demo-drawer__content" style="padding: 10px 15px 10px 0;border-top: 1px solid #ededed;">
        <el-form :label-position="labelPosition" label-width="80px" :model="form">
          <el-form-item label="编号">
            <el-input placeholder="请输入设备编号" clearable v-model="form.serial_number" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="名称">
            <el-input placeholder="请输入设备名称" clearable v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer" style="text-align: center;">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="toSubmit">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { get, post, del } from '@/utils/request'
import _ from 'lodash'

export default {
  data() {
    return {
      title: '录入工程信息',
      visible: false,
      form: {},
      formLabelWidth: '120px',
      labelPosition: 'right',
      drawer: false,
      direction: 'rtl',
      formSearch: {},
      empData: [],
      params: {
        page: 1,
        pageSize: 10
      }
    }
  },
  methods: {
    toSearch() {
      this.params.page = 1;
      this.getEmpData();
    },
    toEdit(row) {
      this.form = {...row };
      this.drawer = true;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
       .then(_ => {
          done();
        })
       .catch(_ => {});
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          let res = await del('/device/deleteById', { id: id });
          this.params.page = 1;
          this.getEmpData();
          this.$message({
            type:'success',
            message: res.message
          });
        } catch (error) {
          this.$message({
            type: 'error',
            message: '删除失败，请稍后重试'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async getEmpData() {
      let temp = {
       ...this.params,
       ...this.formSearch
      }
      try {
        let res = await get('/device/pageQuery', temp);
        console.log(res, '获取响应');
        this.empData = res.data.list;
      } catch (error) {
        this.$message({
          type: 'error',
          message: '获取数据失败，请稍后重试'
        });
      }
    },
    async toSubmit() {
      try {
        if (this.form.id) {
          // 构造要发送给后端的数据
          const dataToSend = {
            id: this.form.id,
            serial_number: this.form.serial_number,
            name: this.form.name
            // 如果还有其他需要更新的字段，也一并添加进来
          };
          let res = await post('/device/update', dataToSend);
          this.drawer = false;
          this.params.page = 1;
          this.getEmpData();
          this.$message({
            type:'success',
            message: res.message
          });
        } else {
          this.$message({
            type: 'error',
            message: '数据异常，无法更新'
          });
        }
      } catch (error) {
        this.$message({
          type: 'error',
          message: '更新失败，请稍后重试'
        });
      }
    }
  },
  created() {
    this.getEmpData();
  }
}
</script>

<style lang="scss" scoped>
.demo-drawer__footer {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>